using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Skeletons
{
    public partial class FormDBMovimentoSave : Form
    {
        private DB m_rDB;
        private DBTableMovimento m_rTabMovimento;
        private Form1 m_pParent;

        private bool m_bAlreadyLoaded = false;

        public FormDBMovimentoSave(Form1 p_pParent)
        {
            InitializeComponent();

            m_pParent = p_pParent;

            m_rDB = m_pParent.getDB();

            m_rTabMovimento = new DBTableMovimento(m_rDB);

            txtDataCreazione.Text = DateTime.Today.Day + "/" + DateTime.Today.Month + "/" + DateTime.Today.Year;
            txtDataAggiornamento.Text = DateTime.Today.Day + "/" + DateTime.Today.Month + "/" + DateTime.Today.Year;
        }

        private void txtNome_Leave(object sender, EventArgs e)
        {
            if (!m_bAlreadyLoaded && txtNome.Text.Trim().Length > 0)
            {
                if (m_rTabMovimento.load(txtNome.Text.Trim()))
                {
                    txtDescrizione.Text = m_rTabMovimento.Descrizione;
                    txtDataCreazione.Text = m_rTabMovimento.DataCreazione;
                    txtDataAggiornamento.Text = m_rTabMovimento.DataAggiornamento;

                    m_bAlreadyLoaded = true;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            m_rTabMovimento.Nome = txtNome.Text;
            m_rTabMovimento.Descrizione = txtDescrizione.Text;
            m_rTabMovimento.DataAggiornamento = DateTime.Today.Day + "/" + DateTime.Today.Month + "/" + DateTime.Today.Year;
            m_rTabMovimento.XML = m_pParent.getAnimator().ToXmlString("");

            if (!m_rTabMovimento.save())
            {
                System.Windows.Forms.MessageBox.Show("Errore nel salvataggio del DB!");
            }
        }


    }
}